package net.wsfcp.android.http.mvp.model;

import android.support.annotation.NonNull;
import android.util.Log;

import com.google.gson.Gson;

import net.wsfcp.android.base.BaseApplication;
import net.wsfcp.android.base.BaseHttpResult;
import net.wsfcp.android.base.BaseModel;
import net.wsfcp.android.bean.requestbean.ReqChangePasswordBean;
import net.wsfcp.android.http.exception.ApiException;
import net.wsfcp.android.http.exception.ErrorType;
import net.wsfcp.android.http.exception.ExceptionEngine;
import net.wsfcp.android.http.exception.ServerException;
import net.wsfcp.android.http.mvp.subscriber.CommonSubscriber;
import net.wsfcp.android.http.mvp.subscriber.ErrorSubscriber;
import net.wsfcp.android.http.mvp.transformer.CommonTransformer;
import net.wsfcp.android.http.UrlHelper;
import net.wsfcp.android.utils.EncryptUtils;
import net.wsfcp.android.utils.SpUtils;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Shaoboyang on 2018/12/14.
 * 主要做一些数据处理呀,网路请求
 */

public class ChangePasswordModel extends BaseModel {

    public void changePassword(@NonNull String oldpwd, @NonNull String newpwd, @NonNull final ChangePasswordInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");


        String oldPwd = EncryptUtils.getEncryptPassword(oldpwd);
        String newPwd = EncryptUtils.getEncryptPassword(newpwd);

        ReqChangePasswordBean bean = new ReqChangePasswordBean(oldPwd, newPwd);//密码需要加密
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        Log.e("json数据", obj);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.changePassword(UrlHelper.CHANGE_PASSWORD + SpUtils.getInstance().getToken(), body)

                .enqueue(new Callback<BaseHttpResult>() {
                    @Override
                    public void onResponse(Call<BaseHttpResult> call, Response<BaseHttpResult> response) {
                        if (response != null && response.body() != null) {
                            if (response.body().getResultCode() == ErrorType.SUCCESS) {
                                infoHint.changePasswordSuccessInfo();
                            } else {
                                infoHint.changePasswordFailInfo(response.body().getResultMsg());
                            }
                        } else {
                            infoHint.changePasswordFailInfo("失败");
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseHttpResult> call, Throwable t) {
                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.changePasswordFailInfo(e.getMessage());
                    }
                });



//                .compose(new CommonTransformer())
//                .subscribe(new CommonSubscriber(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Object o) {
//                        infoHint.changePasswordSuccessInfo();
//                    }
//
//
//                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Throwable e) {
//                        if(e instanceof ServerException)
//                        infoHint.changePasswordFailInfo(((ServerException)e).message);
//                    }
//                });

    }


    //通过接口产生信息回调
    public interface ChangePasswordInfoHint {
        //登录
        void changePasswordSuccessInfo();

        void changePasswordFailInfo(String str);


    }

}
